package com.first.nell.components.dialog

import android.os.Bundle
import android.view.*
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import com.first.nell.R
import com.first.nell.base.BaseDialogAction
import com.first.nell.databinding.DialogFirstNellMessageBinding

class MessageDialog : DialogFragment(), BaseDialogAction {

    companion object {
        fun showFirstNellDialog(
            fragmentManager: FragmentManager,
            message: String,
            cancel: String = "",
            confirm: String = "OK",
            cancelRes: Int = R.drawable.first_nell_main_red_r4,
            confirmRes: Int = R.drawable.first_nell_main_blue_r4,
            listener: () -> Unit = {},
        ) {
            val dialog = MessageDialog()
            dialog.mFirstNellMessage = message
            dialog.mFirstNellCancel = cancel
            dialog.mFirstNellConfirm = confirm
            dialog.mFirstNellCancelFirstNellRes = cancelRes
            dialog.mFirstNellConfirmFirstNellRes = confirmRes
            dialog.mFirstNellOnFirstNellActionFirstNellListener = listener
            dialog.showNow(fragmentManager, MessageDialog::class.java.simpleName)
        }
    }

    lateinit var mFirstNellViewFirstNellBinding: DialogFirstNellMessageBinding

    private var mFirstNellOnFirstNellActionFirstNellListener: () -> Unit = {}
    private var mFirstNellMessage: String? = null
    private var mFirstNellCancel: String? = null
    private var mFirstNellConfirm: String? = null
    private var mFirstNellCancelFirstNellRes: Int = R.drawable.first_nell_main_red_r4
    private var mFirstNellConfirmFirstNellRes: Int = R.drawable.first_nell_main_blue_r4

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View {
        dialog?.requestWindowFeature(Window.FEATURE_NO_TITLE);
        mFirstNellViewFirstNellBinding = DialogFirstNellMessageBinding.inflate(layoutInflater)

        return mFirstNellViewFirstNellBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initFirstNellSubviews()
        dialog?.setCancelable(true)
        dialog?.setCanceledOnTouchOutside(true)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_TITLE, R.style.Dialog)
    }

    override fun onStart() {
        super.onStart()

        dialog?.window?.let {
            it.attributes.gravity = Gravity.CENTER
            it.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
//            it.setWindowAnimations(R.style.bottomDialogAnimation)
        }

        mFirstNellViewFirstNellBinding.textSubmitButtonTitle.text = mFirstNellMessage
        mFirstNellViewFirstNellBinding.submitButtonCancel.mFirstNellViewFirstNellBinding.textSubmitButton.text = mFirstNellCancel
        mFirstNellViewFirstNellBinding.submitButtonConfirm.mFirstNellViewFirstNellBinding.textSubmitButton.text = mFirstNellConfirm

        mFirstNellViewFirstNellBinding.submitButtonCancel.mFirstNellViewFirstNellBinding.textSubmitButtonBg.setBackgroundResource(
            mFirstNellCancelFirstNellRes
        )
        mFirstNellViewFirstNellBinding.submitButtonConfirm.mFirstNellViewFirstNellBinding.textSubmitButtonBg.setBackgroundResource(
            mFirstNellConfirmFirstNellRes
        )

        mFirstNellCancel?.let {
            if (it.isBlank())
                mFirstNellViewFirstNellBinding.submitButtonCancel.visibility = View.GONE
            else
                mFirstNellViewFirstNellBinding.submitButtonCancel.visibility = View.VISIBLE
        }

        mFirstNellConfirm?.let {
            if (it.isBlank())
                mFirstNellViewFirstNellBinding.submitButtonConfirm.visibility = View.GONE
            else
                mFirstNellViewFirstNellBinding.submitButtonConfirm.visibility = View.VISIBLE
        }
    }

    override fun initFirstNellSubviews() {
        try {
            mFirstNellViewFirstNellBinding.submitButtonConfirm.setOnClickListener {
                mFirstNellOnFirstNellActionFirstNellListener.invoke()
                dismiss()
            }
            mFirstNellViewFirstNellBinding.submitButtonCancel.setOnClickListener {
                dismiss()
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    override fun showFirstNellWithFirstNellUIFirstNellTag(gTag: String, fragmentManager: FragmentManager) {

    }

    override fun setFirstNellOnFirstNellActionFirstNellClickFirstNellListener(listener: (Int) -> Unit) {

    }

}